package com.all.mongodb.controller;

import com.all.mongodb.model.User;
import com.all.mongodb.service.UserService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author LY
 * @create 2024/2/27 14:52
 * @Description UserController
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getUsers")
    public List<User> getUsers() {
        log.info("获取用户信息列表|getUsers");
        return userService.getUsers();
    }

    @PostMapping("/saveUser")
    public User saveUser(@RequestBody User user) {
        log.info("保存用户信息|saveUser");
        return userService.createUser(user);
    }

    @DeleteMapping("/deleteUser/{id}")
    public void deleteUser(@PathVariable String id) {
        log.info("删除用户信息|deleteUser");
        userService.deleteUser(id);
    }

    @PutMapping("/updateUser/{id}")
    public void updateUser(@PathVariable String id, User user) {
        log.info("更新用户信息|updateUser");
        userService.updateUser(id, user);
    }

    /**
     * 根据用户 id查找
     * 存在返回，不存在返回 null
     */
    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable String id) {
        log.info("获取单个用户信息|getUser");
        return userService.getUser(id).orElse(null);
    }

    /**
     * 根据年龄段来查找
     */
    @GetMapping("/age/{from}/{to}")
    public List<User> getUserByAge(@PathVariable Integer from, @PathVariable Integer to) {
        log.info("根据年龄段来查找用户信息|getUserByAge");
        return userService.getUserByAge(from, to);
    }

    /**
     * 根据用户名查找
     */
    @GetMapping("/name/{name}")
    public List<User> getUserByName(@PathVariable String name) {
        log.info("根据用户名查找用户信息|getUserByName");
        return userService.getUserByName(name);
    }

    /**
     * 根据用户描述模糊查找
     */
    @GetMapping("/description/{description}")
    public List<User> getUserByDescription(@PathVariable String description) {
        log.info("根据用户描述模糊查找用户信息|getUserByDescription");
        return userService.getUserByDescription(description);
    }

    /**
     * 根据多个检索条件查询
     */
    @GetMapping("/condition")
    public Page<User> pageUser(int size, int page, User user) {
        log.info("根据多个检索条件查询|pageUser");
        return userService.pageUser(size, page, user);
    }
}
